package org.mcache;

import java.io.InputStream;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

/**
 * @Author: tjc
 * @description
 * @Date Created in 23:20 2022-08-11
 */
public class RedisClient {
    private RedisString name;
    private RedisDb db;
    private SocketChannel sc;
    private RedisString[] args;
    private int argc;
    private RedisString queryBuf;
    private int flags;
    private RedisCommand cmd;
    private String buf; // 输出缓冲区

    public void addReplyNil() {
        buf = ClientResponse.nil().asString();
    }

    public void addReplySimpleString(String reply) {
        buf = ClientResponse.simpleString(reply).asString();
    }

    public void addReplyError(String msg) {
        // 1.full client buf
        // 2. create write event
        buf = ClientResponse.error(msg).asString();
    }

    public void addReplyOk() {
        buf = ClientResponse.ok().asString();
    }

    public void addReplyBluk(String value) {
        buf = ClientResponse.bulkString(value).asString();
    }

    public RedisDb getDb() {
        return db;
    }

    public RedisString[] getArgs() {
        return args;
    }

    public void setArgs(RedisString[] args) {
        this.args = args;
    }

    public RedisString getArg(int i) {
        return args[i];
    }

    public RedisString[] getSubArg(int from) {
        return getSubArg(from, args.length);
    }

    public RedisString[] getSubArg(int from, int to) {
        return Arrays.copyOfRange(this.args, from, to);
    }

    public void reply() {
    }

    public RedisString getName() {
        return name;
    }

    public void setName(RedisString name) {
        this.name = name;
    }

    public void setDb(RedisDb db) {
        this.db = db;
    }

    public SocketChannel getSc() {
        return sc;
    }

    public void setSc(SocketChannel sc) {
        this.sc = sc;
    }

    public int getArgc() {
        return argc;
    }

    public void setArgc(int argc) {
        this.argc = argc;
    }

    public RedisString getQueryBuf() {
        return queryBuf;
    }

    public void setQueryBuf(RedisString queryBuf) {
        this.queryBuf = queryBuf;
    }

    public int getFlags() {
        return flags;
    }

    public void setFlags(int flags) {
        this.flags = flags;
    }

    public RedisCommand getCmd() {
        return cmd;
    }

    public void setCmd(RedisCommand cmd) {
        this.cmd = cmd;
    }

    public String getBuf() {
        return buf;
    }

    public void setBuf(String buf) {
        this.buf = buf;
    }

    public void addReplyInt(int number) {
         buf = ClientResponse.integer(number).asString();
    }
}
